using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;
using System.Data.SqlClient;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint.Administration;

namespace TweakSP.Web.Admin
{
    public partial class TweakSP_ActivatedFeatures : GlobalAdminPageBase
    {
        protected SPGridView activatedFeatures;
        protected Label feedback;
        const string ALL_SITE_FEATURES = "SELECT distinct FeatureId, WebId, SiteId FROM Features"; // WHERE WebId = '00000000-0000-0000-0000-000000000000'";


        protected void Page_Load(object sender, EventArgs e)
        {
            if (ContentService.TweakSP_ContentService.Local.DatabaseTweaksEnabled == false)
            {
                base.RedirectToErrorPage("Database Functionality has not been enabled.  Please visit the TweakSP Admin Settings page to enable Database Functionality.");
            }


            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                StringBuilder output = new StringBuilder();

                SPWebApplication webApp = SPContext.Current.Site.WebApplication;

                #region Add SPGridView Columns

                SPBoundField col = new SPBoundField();
                col.DataField = "FeatureId";
                col.HeaderText = "Feature Id";
                activatedFeatures.Columns.Add(col);

                col = new SPBoundField();
                col.DataField = "WebId";
                col.HeaderText = "Web Id";
                activatedFeatures.Columns.Add(col);

                col = new SPBoundField();
                col.DataField = "SiteId";
                col.HeaderText = "Site Id";
                activatedFeatures.Columns.Add(col);

                #endregion Add SPGridView Columns

                SqlConnection conn = null;
                SqlCommand cmd = null;


                DataTable featureTable = new DataTable();
                featureTable.Columns.Add("FeatureId");
                featureTable.Columns.Add("WebId");
                featureTable.Columns.Add("SiteId");

                try
                {
                    foreach (SPContentDatabase cdb in webApp.ContentDatabases)
                    {
                        try
                        {
                            output.Append("Adding new content database: '" + cdb.DatabaseConnectionString + "'");
                            output.Append("<br />");

                            conn = new SqlConnection(cdb.DatabaseConnectionString);
                            cmd = new SqlCommand(ALL_SITE_FEATURES, conn);

                            conn.Open();
                            SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                            while (rdr.Read())
                            {
                                DataRow row = featureTable.NewRow();
                                row["FeatureId"] = rdr["FeatureId"].ToString();
                                row["WebId"] = rdr["WebId"].ToString();
                                row["SiteId"] = rdr["SiteId"].ToString();
                                featureTable.Rows.Add(row);
                            }
                            conn.Close();
                        }
                        catch (Exception ex)
                        {
                            output.Append("Error: " + ex.Message);
                        }
                        finally
                        {
                            if (conn != null)
                            {
                                conn.Close();
                            }
                        }
                    }


                    activatedFeatures.DataSource = featureTable;
                    activatedFeatures.DataBind();

                }
                catch (Exception ex)
                {
                    output.Append("Error: " + ex.Message);
                }

                feedback.Text = output.ToString();
            });

        }

    }
}
